home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1805 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  76 lines

  1. Newsgroups: comp.lang.c
  2. Path: thinkage.on.ca!atbowler
  3. From: atbowler@thinkage.on.ca (Alan Bowler)
  4. Subject: Re: Random numbers...?
  5. Message-ID: <DLApIv.8IA@thinkage.on.ca>
  6. Sender: news@thinkage.on.ca
  7. Organization: Thinkage Ltd.
  8. References: <30DDBEB1.4C42@aruba.ccit.arizona.edu> <DKDz1v.736@eskimo.com> <4d2c6o$h7l@maureen.teleport.com>
  9. Date: Tue, 16 Jan 1996 22:37:43 GMT
  10.  
  11. In article <4d2c6o$h7l@maureen.teleport.com> muse@teleport.com (Andrew T Millard) writes:
  12. >mAg (mag@eskimo.com) wrote:
  13. >: In article <30DDBEB1.4C42@aruba.ccit.arizona.edu> (Sun, 24 Dec 1995 13:57:21 -0700), 
  14. >: vaughn@aruba.ccit.arizona.edu says :
  15. >: >
  16. >: >Hello,
  17. >: >Thanks to those who helped me with my last question on 
  18. >: >cache-flushing. I realize I'm showing my newness to C by even asking 
  19. >: >this, but I need to get some code that will simply generate a random 
  20. >: >integer from 0 to 9. Specifically, I want to generate a string of 
  21. >: >four random digits. I keep generating numbers that look suspiciously 
  22. >: >non-random somehow (seeding the random number generator with the 
  23. >: >current time). Any ideas?
  24. >: >
  25. >: >Thanks,
  26. >: >Bill Vaughn
  27. >
  28. >Bill, this routine should do fine:
  29. >
  30. >#include <stdio.h>
  31. >#include <math.h>
  32.  
  33. You want "#include <stdlib.h>" to define rand(), srand() and RAND_MAX
  34.  
  35. >
  36. >void main(void)
  37.  
  38. NEVER define "main" as returning "void".  This is always incorrect
  39. even if some implementations don't blow up.
  40.  
  41. >{
  42. >    int iCount, digit[4];
  43. >
  44. >    srand(time());   /*  Seeds random numbers  */    
  45.  
  46. But you need "#include <time.h>" to do this, and time()
  47. needs the argument NULL to be used like this.
  48.  
  49. >    for(iCount = 0; iCount <= 3; iCount++)
  50. >    {
  51. >        digit[iCount] = abs(rand() % 10);
  52.                         ^^^
  53. "rand" always returns a positive value so you don't need to
  54. call "abs" on the result.
  55. >    }
  56. >}
  57. >
  58.  
  59. Is is common for rand() to be implemented with a very fast algorithm
  60. that does not work well with  "%" to select the lower bits.  Instead
  61. you want
  62.        
  63.        (int) (rand() /(((double)RAND_MAX + 1) / 10)
  64.  
  65. This is a little cumbersome so the following is easier and
  66. usually faster.
  67.  
  68.       while (10 <= (rval = rand()/(RAND_MAX/10));
  69.       digit[iCount] =  rval;
  70.  
  71. The "while" will normally only execute once, but very occasionally
  72.   rand()/(RAND_MAX/10)
  73. will produce the value 10.  Putting it in the "while" just discards
  74. those few cases.
  75. )
  76.